﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


using Microsoft.AspNet.Identity.EntityFramework;

using System.Data.Entity;
using System.Data.Entity.ModelConfiguration;
using System.Reflection;
using Backend.Data.Entities;
using Backend.Data.Managers;

namespace Backend.Data
{
    public class BackendDbContext : IdentityDbContext<ApplicationUser>
    {
        public BackendDbContext()
            : base("DefaultConnection")
        {

        }

        static BackendDbContext()
        {
            // Set the database intializer which is run once during application start
            // This seeds the database with admin user credentials and admin role
            //Database.SetInitializer<BackendDbContext>(new ApplicationDbInitializer());
        }
        


        public static BackendDbContext Create()
        {
            return new BackendDbContext();
        }

        public DbSet<NaturalPerson> NaturalPersons { get; set; }
        //public DbSet<ApplicationRole> Roles { get; set; }

        //public DbSet<ApplicationUser> ApplicationUsers { get; set; }

        public DbSet<State> States { get; set; }
        public DbSet<City> Citys { get; set; }

        public DbSet<LegalEntity> LegalEntitys { get; set; }
        public DbSet<GeoReferencia> GeoReferencias { get; set; }
        public DbSet<Ubicacion> Ubicacions { get; set; }
        public DbSet<Organigrama> Organigramas { get; set; }
        public DbSet<GeoReferenciaOrganigrama> GeoReferenciaOrganigramas { get; set; }


        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            var typesToRegister = Assembly.GetExecutingAssembly().GetTypes()
           .Where(type => !String.IsNullOrEmpty(type.Namespace))
           .Where(type => type.BaseType != null && type.BaseType.IsGenericType
                && type.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>));
            foreach (var type in typesToRegister)
            {
                dynamic configurationInstance = Activator.CreateInstance(type);
                modelBuilder.Configurations.Add(configurationInstance);
            }
            base.OnModelCreating(modelBuilder);
            //modelBuilder.Entity<IdentityRole>().Property(p => p.Id).HasColumnName("Id");
            //modelBuilder.Entity<ApplicationRole>().Property(p => p.Id).HasColumnName("Id");
        }


    }
}
